package com.lanou.controller;

import com.lanou.bean.Blog;
import com.lanou.bean.User;
import com.lanou.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * Created by dllo on 18/2/26.
 */
@Controller
public class UserController {
    @RequestMapping(value = {"index", "", "/", "login"})
    private String login() {
        return "login";
    }

    @RequestMapping(value = "register")
    private String reg() {
        return "register";
    }

    @Resource
    private UserService userService;

    @ResponseBody
    @RequestMapping(value = "/res")
    public String register(@RequestParam("username") String username, @RequestParam("password") String password, @RequestParam("phone_number") String tel, @RequestParam("email") String address) {
        User user = new User();
        user.setName(username);
        user.setPassword(password);
        user.setTel(tel);
        user.setAddress(address);
        userService.regist(user);
        System.out.println("用户注册：" + user.getName() + user.getPassword());
        return "success";
    }

    @RequestMapping(value = "/log")
    public String log(@RequestParam("username") String name, @RequestParam("password") String password,HttpSession session) throws Exception {
        User user = userService.login(name, password);
        System.out.println("用户登录：" + name + password);
        if (user != null) {
            session.setAttribute("user", user.getName());
            return "blog";
        }
        return "fail";
    }
    @ResponseBody
    @RequestMapping(value = "/name")
    public String username(HttpSession session){
        String name=session.getAttribute("user").toString();
        System.out.println(name);
        return name;
    }
}
